package demo.stock

@Grapes([
	@GrabResolver(name='aliyun', root='https://maven.aliyun.com/repository/central',m2Compatible='true'),
	@GrabResolver(name='oss', root='https://oss.sonatype.org/content/repositories/snapshots',m2Compatible='true'),
	@GrabResolver(name='oss2', root='https://s01.oss.sonatype.org/content/repositories/snapshots',m2Compatible='true'),
	@Grab("mysql:mysql-connector-java:8.0.19"),
	@Grab("org.jsoup:jsoup:1.15.3"),
	@Grab("com.mchange:c3p0:0.9.5.5")
])
import java.io.Serializable;
import java.util.List;
import org.apache.oro.text.perl.Perl5Util;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import java.text.DecimalFormat;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import cool.taomu.utils.JdbcUtils;
import cool.taomu.lang.TaomuEntry;

class StockList implements TaomuEntry{
	static ComboPooledDataSource dataSource = new ComboPooledDataSource();
	static{
		dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql://mysql.sqlpub.com:3306/test_stock?useUnicode=true");
		dataSource.setUser("test_stock");
		dataSource.setPassword("f5e34a28faf29590");
		dataSource.setAcquireIncrement(2);
		dataSource.setInitialPoolSize(5);
		dataSource.setMinPoolSize(2);
		dataSource.setMaxPoolSize(5);
	}
    public void entry(String payload){
    	List<StockDataEntity> sdelist = new JdbcUtils().query(dataSource.getConnection(),"select * from stock_data",StockDataEntity.class);
//		System.out.println(SinaStock.getStockData("sh000001"));
		System.out.println(sdelist.size());
    }
}

class StockIndustryEntity {
	String industry;	
	List<String> codes; 
	new(String industry,List<String> codes){
		this.industry = industry;
		this.codes = codes;
	}
}

class StockDataEntity implements Serializable{
	String code;
	String name;
	String industry;
	String industryClassification;
	String updateDate;
	public String toString(){
		return this.code + this.name
	}
}

class StockMarketEntity implements Serializable {
	String code;
	String name;
	float kp;
	float sp;
	float dq;
	float zg;
	float zd;
	float gs;
	float je;
	String date;
	String time;
	float zt;
	float dt;
	float zdf;
	String rgb = "#e53612";
	int sort = 0;

	public StockMarketEntity(String code,String name,float kp,float sp,float dq,float zg,float zd,float gs,float je){
		this.code = code;
		DecimalFormat df = new DecimalFormat(".00");
		this.zt = sp + sp * 0.1f;
		this.zt = Float.parseFloat(df.format(this.zt));
		this.dt = sp - sp * 0.1f;
		this.dt = Float.parseFloat(df.format(this.dt));
		this.zdf = (dq - sp ) / sp * 100;
		this.zdf = Float.parseFloat(df.format(this.zdf));
		this.name = name;
		this.kp = kp;
		this.sp = sp;
		this.dq = dq;
		this.zg = zg;
		
		this.zd = zd;
		this.gs = gs;
		this.je = je;
		if(this.dq > sp){
			this.rgb = "#e53612";
		}else if(this.dq < sp){
			this.rgb = "#358712";
		}else{
			this.rgb = "#5e5757";
		}
	}
	public String toString(){
		return this.name;
	}
}

class SinaStock{
	def static List<StockMarketEntity> getStockData(String code){
		String url = String.format("http://hq.sinajs.cn/list=%s",code);
		def connect = Jsoup.connect(url).referrer("http://finance.sina.com.cn");
		def response = connect.maxBodySize(50000000).method(Method.GET).ignoreContentType(true).execute();
		String body = response.body();
		Perl5Util p5 = new Perl5Util();
		PatternMatcherInput input = new PatternMatcherInput(body);
		List<StockMarketEntity> smelist = new ArrayList();
		while(p5.match("/var hq_str_(sh|sz)([0-9]{6})=\"(.*?)\";/",input)){
			String scode = p5.group(2);
			String[] r = p5.group(3).split(",");
			float kp = Float.parseFloat(r[1]);
			if(kp != 0){
				String name = r[0];
				float sp = Float.parseFloat(r[2]);
				float dq = Float.parseFloat(r[3]);
				float zg = Float.parseFloat(r[4]);
				float zd = Float.parseFloat(r[5]);
				float gs = Float.parseFloat(r[8]);
				float je = Float.parseFloat(r[9]);
				StockMarketEntity sme = new StockMarketEntity(scode,name,kp,sp,dq,zg,zd,gs,je);
				sme.date = r[30];
				sme.time = r[31];
				smelist.add(sme);
			}
		}
		return smelist;
	}
}


